from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 导入数据库连接
from SaaSDevelTool.data_mysql_data import db

"""企业已开启功能包查看"""


class SaaSDataTenementPackageQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取企业编号
        tenement_id = request.GET.get("tenement_id")
        # 获取大模块编号
        model_id = request.GET.get("model_id")

        # 判断对应的企业编号
        if not tenement_id:
            return create_response(code=400, msg="企业编号传递错误。")

        # 判断大模块编号
        if not model_id:
            return create_response(code=400, msg="大模块编号传递错误。")

        # 拼写查询语句
        query_map_tenement_package_table_sql = """
            SELECT 
                t.package_id,
                spt.package_name 
            FROM 
                db_saas_center.map_tenement_package_table t
            LEFT JOIN 	
                db_saas_center.sum_package_table spt 
            ON 	
                t.package_id = spt.package_id 
            WHERE 
                spt.model_id = {} 
            AND 
                t.tenement_id = {}
            ;""".format(model_id, tenement_id)

        # 获取数据集合
        query_map_tenement_package_table_data = db.findAll(query_map_tenement_package_table_sql)

        # 返回数据集合
        return create_response(code=200, msg="", data=query_map_tenement_package_table_data)
